package com.yidu.best.TA.Ta.controller;

import java.io.InputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.yidu.best.TA.Ta.domain.Ta;
import com.yidu.best.TA.Ta.domain.TaVo;
import com.yidu.best.TA.Ta.service.TaService;

@Controller
@RequestMapping("/sys/")
public class TaController {
	@Autowired
	private TaService taService;
	
	@RequestMapping("Tamap")
	public String TaMap(){
		return "/sys/Ta/Ta";
	}
	
	@ResponseBody
	@RequestMapping(value = "Tas" )
	public Map<String, Object> findTa(TaVo taVo){
		return taService.findTa(taVo);
	}
	
	@ResponseBody
	@RequestMapping(value = "find", method = RequestMethod.GET)
	public Map<String, Object> find(String ta){
		return taService.find(ta);
	}
	
	@ResponseBody
	@RequestMapping(value = "add" , method = RequestMethod.POST)
	public String add(HttpServletRequest request) throws Exception{
		MultipartHttpServletRequest multipart = (MultipartHttpServletRequest) request;
		MultipartFile file = multipart.getFile("file");
	    InputStream in = file.getInputStream();
	    taService.ajaxUploadExcel(in, file);
	    in.close();
		return "true"; 
	}
	
	@ResponseBody
	@RequestMapping(value = "update" , method = RequestMethod.POST)
	public String updateFundTrade(@RequestBody Ta ta,String ids){
		String[] s=ids.split(",");
		int i = 0;
		for (String fundTradeId : s) {
			i += taService.updateFundTrade(fundTradeId);
		}
		if (i > 0) {
			taService.insertCapitalTransfer(ta);
			taService.insertFundinventory(ta);
			taService.insertCashinventory(ta);
			return "结算完成";
		}
		return "结算失败";
	}
	
	@ResponseBody
	@RequestMapping(value = "fan" , method = RequestMethod.POST)
	public String fanjiesuan(String ids){
		String[] s=ids.split(",");
		int i = 0;
		for (String fundTradeId : s) {
			i += taService.fanjiesuan(fundTradeId);
		}
		if (i > 0) {
			return "结算完成";
		}
		return "结算失败";
	}
}
